package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.dao.SyLabelGoodsMapper;
import com.eco.fanliapp.entity.SyLabelGoods;
import com.eco.fanliapp.service.ISyLabelGoodsService;
import com.eco.fanliapp.utils.Convert;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品标签对照表 服务实现类
 */
@Service
public class SyLabelGoodsServiceImpl extends ServiceImpl<SyLabelGoodsMapper, SyLabelGoods> implements ISyLabelGoodsService {

    @Autowired
    private SyLabelGoodsMapper syLabelGoodsMapper;

    @Override
    public int addSyLabelGoods(SyLabelGoods syLabelGoods) {

        QueryWrapper<SyLabelGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", syLabelGoods.getItemId());
        queryWrapper.eq("label_type_id", syLabelGoods.getLabelTypeId());
        queryWrapper.eq("label_code", syLabelGoods.getLabelCode());
        queryWrapper.eq("enable", 1);
        List<SyLabelGoods> labelGoods = syLabelGoodsMapper.selectList(queryWrapper);
        if (labelGoods != null&&labelGoods.size()>0) {
            //修改
            int i = 1;
//            if (syLabelGoods.getDeleted() == 1) {
                syLabelGoods.setTableId(labelGoods.get(0).getTableId());
                i = syLabelGoodsMapper.updateById(syLabelGoods);
//            }
            return i;
        } else {
            //添加
            int i = 1;
            if (syLabelGoods.getDeleted() == 0) {
                i = syLabelGoodsMapper.insert(syLabelGoods);
            }
            return i;
        }
    }

    @Override
    public int deleteByLableCode(String lableCode) {
        return syLabelGoodsMapper.deleteByLableCode(lableCode);
    }
    @Override
    public List<String> findByLabelCode(String lableCode) {
        return syLabelGoodsMapper.findByLabelCode(lableCode);
    }
    @Override
    public int deleteByItemIds(String ids) {
        return syLabelGoodsMapper.deleteByItemIds(Convert.toStrArray(ids));
    }

    @Override
    public SyLabelGoods selectGoodsLabel(String itemid, String activityType) {

        return  syLabelGoodsMapper.selectGoodsLabel(itemid,activityType);
    }

    @Override
    public List<SyLabelGoods> findGoodsLabel(String itemid) {
        return syLabelGoodsMapper.findGoodsLabel(itemid);
    }


}
